package main

import (
	"context"
	"math/rand"
	"time"

	"github.com/TencentBlueKing/bkmonitor-kits/metricsdk"
)

// 全局声明 Metrics 同 prom sdk 类似

var CpuSysUsedTotal metricsdk.CounterVec
var KafkaQueueSize metricsdk.GaugeVec
var HttpRequestSize metricsdk.HistogramVec

func registerVec() {
	CpuSysUsedTotal = metricsdk.MustRegisterCounterVec(metricsdk.CounterVecOpts{
		Name: "cpu_sys_used_total",
		Desc: "cpu system used total in milliseconds",
	}, []string{"idcId", "hostIp", "bizId"})

	KafkaQueueSize = metricsdk.MustRegisterGaugeVec(metricsdk.GaugeVecOpts{
		Name: "kafka_queue_size",
		Desc: "kafka queue size in realtime",
	}, []string{"topic", "instance"})

	HttpRequestSize = metricsdk.MustRegisterHistogramVec(metricsdk.HistogramVecOpts{
		Name: "http_request_body_size",
		Desc: "http request body size in bytes",
	}, []string{"uri", "instance"})
}

func fakeUri() string {
	uri := []string{"/home", "/index", "/users", "/info"}
	return uri[rand.Int31n(int32(len(uri)))]
}

func fakeTopic() string {
	uri := []string{"topic1", "topic2", "topic3", "topic4"}
	return uri[rand.Int31n(int32(len(uri)))]
}

// 业务逻辑处理

func work(ctx context.Context) {
	cpuTick := time.Tick(time.Second * 5)
	kafkaTick := time.Tick(time.Second * 3)
	httpTick := time.Tick(time.Millisecond * 100)

	for {
		select {
		case <-ctx.Done():
			return
		case <-cpuTick:
			CpuSysUsedTotal.Inc("01", "10.0.1.27", "bar")
		case <-kafkaTick:
			KafkaQueueSize.Set(float64(rand.Int31n(1000000)), fakeTopic(), "10.0.1.27")
		case <-httpTick:
			HttpRequestSize.Observe(float64(100+rand.Int31n(200)), fakeUri(), "10.0.1.27")
		}
	}
}
